<?php
namespace App\HttpController\Traits;

trait Category{

    public function category_add(){
        $request=$this->request();
        $post=$request->getParsedBody();
        $model=new \App\Model\CategoryModel();
        if($post){
            $post=\dtrim($post);
            $post['moduleid']=$this->moduleid;
            $post['listorder']=intval($post['listorder']);

            $vali=new \EasySwoole\Validate\Validate();
            $vali->addColumn('moduleid')->lengthMin(1,'mouleid不能为空');
            $vali->addColumn('catname')->lengthMin(1,'分类名称不能为空');
            $vali->addColumn('catanme')->func(function($d) use($model){
                $data=$model->where(['moduleid'=>$d['moduleid'],'parentid'=>$d['parentid'],'catname'=>$d['catname']])->get();
                if($data) return false;
                return true;
            },'分类名称已经存在');
            $r=$vali->validate($post);
            if(!$r){
                $this->msg($r,$vali->getError()->__toString());
                return;
            }
            $model=\App\Model\CategoryModel::create($post);
            $insertid=$model->save();
            $model->cache($this->moduleid);
            $this->msg($insertid);
            return;
        }
        $parent=$model->category_select($this->moduleid,'parentid',0,'顶级分类',' class="form-control" ');
        $max_catid=$model->max_catid()+1;
        $this->fetch('../Category/category_add',get_defined_vars());
    }

    public function category_lst(){
        $request=$this->request();
        $get=$request->getQueryParams();
        extract($get);

        $cond=[];
		$cond['moduleid']=$this->moduleid;
        
        $model=new \App\Model\CategoryModel();
		$category_arr=$model->where($cond)->select();
		$category_arr=$model->getCatTree($category_arr);

        $this->fetch('../Category/category_lst',get_defined_vars());
    }

    public function category_mod(){
        $request=$this->request();
        $get=$request->getQueryParams();
        extract($get);
        $catid=intval($catid);
		if(!$catid){
            $this->msg(0,'lose catid');
            return;
        }

        $raw=$post=$request->getParsedBody();
        $model=new \App\Model\CategoryModel();
        if($post){
            unset($post['submit'],$post['FORWARD']);
            $post=\dtrim($post);
            $post['moduleid']=$this->moduleid;
            $post['listorder']=intval($post['listorder']);

            $vali=new \EasySwoole\Validate\Validate();
            $vali->addColumn('moduleid')->lengthMin(1,'mouleid不能为空');
            $vali->addColumn('catname')->lengthMin(1,'分类名称不能为空');
            $vali->addColumn('catanme')->func(function($d) use($model,$catid){
                $data=$model->where("catid!='{$catid}'")->where(['moduleid'=>$d['moduleid'],'parentid'=>$d['parentid'],'catname'=>$d['catname']])->get();
                if($data) return false;
                return true;
            },'分类名称已经存在');
            $r=$vali->validate($post);
            if(!$r){
                $this->msg($r,$vali->getError()->__toString());
                return;
            }
            $r=$model->mod_check_ok($this->moduleid,$catid,$post['parentid']);
			if(!$r){
                $this->msg(0,'上级分类选择错误');
                return;
            }
            $model=\App\Model\CategoryModel::create();
            $cat=$model->get($catid);
            $r=$cat->update($post);
            $model->cache($this->moduleid);
            $this->msg($r,'',$raw['FORWARD']);
            return;
        }
        $data=$model->where(['moduleid'=>$this->moduleid,'catid'=>$catid])->get();
		if(!$data){
            $this->msg(0,'分类不存在');
            return;
        }
        $parent=$model->category_select($this->moduleid,'parentid',$data['parentid'],'顶级分类',' class="form-control" ');
        $this->fetch('../Category/category_mod',get_defined_vars());
    }

    public function category_del(){
        $request=$this->request();
        $get=$request->getQueryParams();
        extract($get);

        $catid=intval($catid);
		if(!$catid){
            $this->msg(0,'lose catid');
            return;
        }
		$model=new \App\Model\CategoryModel();
		$r=$model->del_check_ok($catid,$this->moduleid);
        if(!$r){
            $this->msg(0,'该分类无法删除，请先删除其子分类，并删除其下文章');
            return;
        }
        $model=\App\Model\CategoryModel::create();
        $cat=$model->where(['moduleid'=>$this->moduleid,'catid'=>$catid])->get();
        if(!$cat){
            $this->msg(0,'数据不存在');
            return;
        }
        $r=$cat->destroy();
		$model->cache($this->moduleid);
		$this->msg($r===false?false:true);
    }

    public function category_cache(){
        $model=new \App\Model\CategoryModel();
        $model->cache($this->moduleid);
        $this->msg(1);
    }

    public function category_outside(){
        $request=$this->request();
        $get=$request->getQueryParams();
        extract($get);

        if(!isset($callback)){
            $this->msg(0,'lose callback');
            return;
        }

        $cond=[];
		$cond['moduleid']=$this->moduleid;
        
        $model=new \App\Model\CategoryModel();
		$category_arr=$model->where($cond)->select();
		$category_arr=$model->getCatTree($category_arr);

        $this->fetch('../Category/category_outside',get_defined_vars());
    }

}